//
//  JDPopoverPresentationController.swift
//  JDWeiBo
//
//  Created by apple on 15/7/1.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class JDPopoverPresentationController: UIPresentationController {
    
    // 遮盖视图
    lazy var dummyView: UIView = {
       
        let v = UIView()
        
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        return v
    }()
    
    ///  实例化转场控制器
    ///
    ///  :param: presentedViewController  展示的控制器
    ///  :param: presentingViewController 发起的控制器
    ///
    ///  :returns: 展示
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        prepareDummyView()
    }
    
    private func prepareDummyView() {
        let tap = UITapGestureRecognizer(target: self, action: "close")
        dummyView.addGestureRecognizer(tap)
    }
    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 容器重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 添加并且设置 dummyView
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex: 0)
        
        // 设置视图大小
        
//        let x = (view.bounds.width - 220) / 2
        presentedView()?.frame = CGRectMake(78, 56, 220, 280)
    }
}
